using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using HunterPluginSDK;
using HunterPluginSDK.Interface;


namespace MyHunter.UI
{
    public partial class Settings : Form
    {
        public Settings()
        {
            InitializeComponent();

            this.defaultBindingSource.DataSource = DataSchema.Settings.SettingsMgr.Defaults;
            this.myCollectionBindingSource.DataSource = DataSchema.Settings.SettingsMgr.WatcherSettings;

            //this.defaultBindingSource.DataSource = DataSchema.Settings.SettingsMgr.Settings.Default;
            //this.defaultBindingSource.DataMember = "Default";
            comboBoxLogPrior.DataSource = Enum.GetValues(typeof(LogPriority));
            //this.comboBoxLogPrior.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.defaultBindingSource, "LogPriority", true));
            Binding b = new System.Windows.Forms.Binding("SelectedIndex", this.defaultBindingSource, "LogPriority", true);
            //b.Parse +=new ConvertEventHandler(b_Parse);
            //b.Format +=new ConvertEventHandler(b_Format);
            this.comboBoxLogPrior.DataBindings.Add(b);

            InitPlugins();
        }

        private void InitPlugins()
        {
            foreach (HunterPluginSDK.Interface.IHunterPlugin hpi in HunterPluginSDK.PluginsFactory.Instance.PluginsList)
            {
                this.listBoxPlugins.Items.Add(hpi);
            }
        }

        void b_Format(object sender, ConvertEventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void b_Parse(object sender, ConvertEventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        #region EVENTS
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            defaultBindingSource.EndEdit();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void OnSelectionCommited(object sender, EventArgs e)
        {
            foreach(Binding binding in comboBoxLogPrior.DataBindings)
            {
                binding.WriteValue();
            }
        }

        private void buttonBrowseCollectionPath_Click(object sender, EventArgs e)
        {
            if(this.folderBrowserDialog.ShowDialog() == DialogResult.OK)
                this.collectionPathTextEdit.Text = folderBrowserDialog.SelectedPath;
        }
        #endregion

        private void onPluginDoubleClick(object sender, EventArgs e)
        {
            CheckedListBox lb = sender as CheckedListBox;
            if (lb != null)
            {
                HunterPluginSDK.Interface.IHunterPlugin hpi = lb.SelectedItem as HunterPluginSDK.Interface.IHunterPlugin;
                if (hpi != null)
                    hpi.DoSetup();
            }
        }

    }
}